package com.travel;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.readystatesoftware.systembartint.SystemBarTintManager;

import java.util.Timer;
import java.util.TimerTask;

import fragments.DiscoverFragment;
import fragments.MineFragment;
import fragments.PhotosFragment;
import fragments.ShopFragment;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{
    private RadioGroup rg;
    //fragment管理器
    FragmentManager Manager;
    private DiscoverFragment mDicFragment;
    private PhotosFragment mPhoFragment;
    private ShopFragment mShopFragment;
    private MineFragment mMineFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(R.color.statusbar_bg);//通知栏所需颜色
            initView();
        inData();
    }

    private void inData() {
        Selection(0);
    }
    private void Selection(int index ) {
        FragmentTransaction ft= Manager.beginTransaction();//开启Fragment的事务
        hindFragment(ft);
        switch (index){
            case 0:
                if (mDicFragment==null){
                    mDicFragment=new DiscoverFragment();
                    ft.add(R.id.main_layout,mDicFragment);
                }else{
                    ft.show(mDicFragment);
                }
                break;
            case 1:
                if (mPhoFragment==null){
                    mPhoFragment=new PhotosFragment();
                    ft.add(R.id.main_layout,mPhoFragment);
                }else{
                    ft.show(mPhoFragment);
                }
                break;
            case 2:
                if (mShopFragment==null){
                    mShopFragment=new ShopFragment();
                    ft.add(R.id.main_layout,mShopFragment);
                }else{
                    ft.show(mShopFragment);
                }
                break;
            case 3:
                if (mMineFragment==null){
                    mMineFragment=new MineFragment();
                    ft.add(R.id.main_layout,mMineFragment);
                }else{
                    ft.show(mMineFragment);
                }
                break;
        }
        ft.commit();
    }

    private void hindFragment(FragmentTransaction ft) {
        if(mDicFragment!=null){
            ft.hide(mDicFragment);
        }
        if(mPhoFragment!=null){
            ft.hide(mPhoFragment);
        }
        if(mShopFragment!=null){
            ft.hide(mShopFragment);
        }
        if(mMineFragment!=null){
            ft.hide(mMineFragment);
        }
    }

    private void initView() {
        Manager=getSupportFragmentManager();
        rg= (RadioGroup) findViewById(R.id.rg_main);
        rg.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i){
            case R.id.rb_dis_btn:
                Selection(0);
                break;
            case R.id.rb_pht_btn:
                Selection(1);
                break;
            case R.id.rb_shop_btn:
                Selection(2);
                break;
            case R.id.rb_mess_btn:
                Selection(3);
                break;
        }
    }
    /**
     * 菜单、返回键响应
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if(keyCode == KeyEvent.KEYCODE_BACK)
        {
            exitBy2Click(); //调用双击退出函数
        }
        return false;
    }
    /**
     * 双击退出函数
     */
    private static Boolean isExit = false;

    private void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
            isExit = true; // 准备退出
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务

        } else {
            finish();
            System.exit(0);
        }
    }
    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }
}
